using System;
using System.Collections.Generic;
using System.Windows.Browser;

namespace HtmlUtilities
{
    public interface IHtmlDocment : IHtmlObject
    {
        IHtmlElement Body { get; }
        string Cookies { get; }
        IHtmlElement CreateElement(string tagName);
        IHtmlElement DocumentElement{ get; }
        event EventHandler DocumentReady;
        Uri DocumentUri { get; }
        IHtmlElement GetElementById(string id);
        IScriptObjectCollection GetElementsByTagName(string tagName);
        bool IsReady { get; }
        IDictionary<string, string> QueryString { get; }
        void Submit();
        void Submit(string formId);
    }

    internal class HtmlDocumentWrapper : HtmlObjectWrapper, IHtmlDocment
    {
        private readonly HtmlDocument _document;
        public HtmlDocumentWrapper(HtmlDocument document) : base(document)
        {
            _document = document;
        }

        public IHtmlElement Body
        {
            get { return ScriptObjectFactory.GetScriptObject<IHtmlElement>(_document.Body); }
        }

        public string Cookies
        {
            get { return _document.Cookies; }
        }

        public IHtmlElement CreateElement(string tagName)
        {
            return ScriptObjectFactory.GetScriptObject<IHtmlElement>(_document.CreateElement(tagName));
        }

       public IHtmlElement DocumentElement
        {
            get { return ScriptObjectFactory.GetScriptObject<IHtmlElement>(_document.DocumentElement); }
        }

        public event EventHandler DocumentReady
        {
            add { _document.DocumentReady += value; }
            remove { _document.DocumentReady -= value; }
        }

        public Uri DocumentUri
        {
            get { return _document.DocumentUri; }
        }

        public IHtmlElement GetElementById(string id)
        {
            var element = _document.GetElementById(id);
            return element != null ? ScriptObjectFactory.GetScriptObject<IHtmlElement>(element) : null;
        }

        public IScriptObjectCollection GetElementsByTagName(string tagName)
        {
            return ScriptObjectFactory.GetScriptObject<IScriptObjectCollection>(_document.GetElementsByTagName(tagName));
        }

        public bool IsReady
        {
            get { return _document.IsReady; }
        }

        public IDictionary<string, string> QueryString
        {
            get { return _document.QueryString; }
        }

        public void Submit()
        {
            _document.Submit();
        }

        public void Submit(string formId)
        {
            _document.Submit(formId);
        }
    }
}